#!/usr/bin/python

config = []
for line in open('/etc/uppydate/config'):
    line = line.rstrip()
    config.append(line)

from gi.repository import Gtk
import os
from os.path import expanduser

class ListBoxWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Uppydate Configuration")
        self.set_border_width(8)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_icon_from_file('/usr/share/uppydate/uppydate.png')
        hbox = Gtk.Box(spacing=8)
        self.add(hbox)
        listbox = Gtk.ListBox()
        listbox.set_selection_mode(Gtk.SelectionMode.NONE)
        hbox.pack_start(listbox, True, True, 0)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        label1 = Gtk.Label("delay from login to first update check (in seconds)", xalign=0)
        adjustment = Gtk.Adjustment(0, 0, 999999999, 1, 10, 0)
        policy = Gtk.SpinButtonUpdatePolicy.IF_VALID
        delayonevalue = eval(config[0])
        self.spinbutton1 = Gtk.SpinButton()
        self.spinbutton1.set_adjustment(adjustment)
        self.spinbutton1.set_update_policy(policy)
        self.spinbutton1.set_value(delayonevalue)
        hbox.pack_start(label1, True, True, 0)
        hbox.pack_start(self.spinbutton1, False, False, 0)
        listbox.add(row)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        label2 = Gtk.Label("delay between subsequent update checks (in seconds)", xalign=0)
        adjustment = Gtk.Adjustment(0, 0, 999999999, 1, 10, 0)
        policy = Gtk.SpinButtonUpdatePolicy.IF_VALID
        delaytwovalue = eval(config[1])
        self.spinbutton2 = Gtk.SpinButton()
        self.spinbutton2.set_adjustment(adjustment)
        self.spinbutton2.set_update_policy(policy)
        self.spinbutton2.set_value(delaytwovalue)
        hbox.pack_start(label2, True, True, 0)
        hbox.pack_start(self.spinbutton2, False, False, 0)
        listbox.add(row)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        label = Gtk.Label("notification text when updates are available", xalign=0)
        self.entry1 = Gtk.Entry()
        self.entry1.set_text("%s" % config[2])
        hbox.pack_start(label, True, True, 0)
        hbox.pack_start(self.entry1, True, True, 0)
        listbox.add(row)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        label = Gtk.Label("notification text when system is up to date", xalign=0)
        self.entry2 = Gtk.Entry()
        self.entry2.set_text("%s" % config[3])
        hbox.pack_start(label, True, True, 0)
        hbox.pack_start(self.entry2, True, True, 0)

        listbox.add(row)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        label = Gtk.Label("notification icon when updates are available", xalign=0)
        self.button1 = Gtk.Button("%s" % (config[4]))
        self.button1.connect("clicked", self.button1sig)
        hbox.pack_start(label, True, True, 0)
        hbox.pack_start(self.button1, False, True, 0)

        listbox.add(row)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        label = Gtk.Label("notification icon when system is up to date", xalign=0)
        self.button2 = Gtk.Button("%s" % (config[5]))
        self.button2.connect("clicked", self.button2sig)
        hbox.pack_start(label, True, True, 0)
        hbox.pack_start(self.button2, False, True, 0)

        listbox.add(row)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        label = Gtk.Label("show \"system is up to date\" notifications", xalign=0)
        self.check = Gtk.CheckButton()
        if config[6] == "True":
            self.check.set_active(True)
        elif config[6] == "False":
            self.check.set_active(False)
        hbox.pack_start(label, True, True, 0)
        hbox.pack_start(self.check, False, True, 0)

        listbox.add(row)

        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50, homogeneous=True)
        row.add(hbox)

        gnome = os.path.isdir('/usr/share/gnome')
        if gnome == True:
            autofile = os.path.exists(os.path.expanduser('~/.config/autostart/uppydate-launcher.desktop'))
            if autofile == False:
                self.autobutton = Gtk.Button("Enable Autostart")
            elif autofile == True:
                self.autobutton = Gtk.Button("Disable Autostart")
            self.autobutton.connect("clicked", self.autostart)
            hbox.pack_start(self.autobutton, True, True, 0)

        button = Gtk.Button("Restore Defaults")
        button.connect("clicked", self.default)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button("Cancel")
        button.connect("clicked", self.cancel)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button("Done")
        button.connect("clicked", self.done)
        hbox.pack_start(button, True, True, 0)

        listbox.add(row)

    def button1sig(self, configalter):
        dialog = Gtk.FileChooserDialog("Choose Icon", self,
            Gtk.FileChooserAction.OPEN,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            configalter.set_label(dialog.get_filename())
            iconfile1 = dialog.get_filename()
        dialog.destroy()

    def button2sig(self, configalter):
        dialog = Gtk.FileChooserDialog("Choose Icon", self,
            Gtk.FileChooserAction.OPEN,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            configalter.set_label(dialog.get_filename())
            iconfile2 = dialog.get_filename()
        dialog.destroy()

    def autostart(self, button):
        autofile = os.path.exists((os.path.expanduser('~/.config/autostart/uppydate-launcher.desktop')))
        if autofile == False:
            if not os.path.exists(os.path.expanduser('~/.config/autostart')):
                os.makedirs(os.path.expanduser('~/.config/autostart'))
            os.symlink('/usr/share/uppydate/uppydate-launcher.desktop',os.path.expanduser('~/.config/autostart/uppydate-launcher.desktop'))
            self.autobutton.set_label("Disable Autostart")
        elif autofile == True:
            os.remove(os.path.expanduser('~/.config/autostart/uppydate-launcher.desktop'))
            self.autobutton.set_label("Enable Autostart")

    def default(self, button):
        config = ["60", "28800", "NUM system UPD available", "Arch Linux is up to date", "/usr/share/uppydate/arch-stale.png", "/usr/share/uppydate/arch-fresh.png", "True"]
        configfile = open("/etc/uppydate/config", "w")
        for item in config:
            configfile.write("%s\n" % item)
        self.spinbutton1.set_value(60)
        self.spinbutton2.set_value(28800)
        self.entry1.set_text("NUM system UPD available")
        self.entry2.set_text("Arch Linux is up to date")
        self.button1.set_label("/usr/share/uppydate/arch-stale.png")
        self.button2.set_label("/usr/share/uppydate/arch-fresh.png")
        self.check.set_active(True)

    def cancel(self, button):
        Gtk.main_quit()

    def done(self, button):
        config[0] = self.spinbutton1.get_value_as_int()
        config[1] = self.spinbutton2.get_value_as_int()
        config[2] = self.entry1.get_text()
        config[3] = self.entry2.get_text()
        config[4] = self.button1.get_label()
        config[5] = self.button2.get_label()
        config[6] = self.check.get_active()

        configfile = open("/etc/uppydate/config", "w")
        for item in config:
            configfile.write("%s\n" % item)
        Gtk.main_quit()

win = ListBoxWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
